import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test35 {
    public static char firstNotRepeatingChar(String s){
        if (s ==null || s.length() ==0) throw new IllegalArgumentException("invalid");
        Map<Character,Integer> map = new HashMap<>();
        for (int i = 0; i< s.length();i++){
            char c = s.charAt(i);
            if (map.containsKey(c)){
                map.put(c,-1);
            }else map.put(c,i);//如果不存在则把c和索引都放入map
        }

        Set<Map.Entry<Character, Integer>> set = map.entrySet();
        int index = Integer.MAX_VALUE;
        char res = '\0';
        for (Map.Entry<Character,Integer> entry : set){
            if (entry.getValue() < index && entry.getValue() >= 0){
                index =entry.getValue();
                res = entry.getKey();
            }
        }
        return  res;
    }

    public static void main(String[] args) {
        System.out.println(firstNotRepeatingChar("google")); // l
        System.out.println(firstNotRepeatingChar("aabccdbd")); // '\0'
        System.out.println(firstNotRepeatingChar("abcdefg")); // a
        System.out.println(firstNotRepeatingChar("gfedcba")); // g
        System.out.println(firstNotRepeatingChar("zgfedcba")); // g
    }
}
